package cn.jx.asc.service.impl;

import cn.jx.asc.dao.mapper.MountainMapper;
import cn.jx.asc.pojo.bo.MountainBO;
import cn.jx.asc.pojo.bo.PipeInfoBO;
import cn.jx.asc.pojo.entity.Mountain;
import cn.jx.asc.pojo.vo.MountainVO;
import cn.jx.asc.service.MountainService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lhg
 * @since 2021-06-28
 */
@Service
public class MountainServiceImpl extends ServiceImpl<MountainMapper, Mountain> implements MountainService {

    @Autowired
    private MountainMapper mountainMapper;

    @Transactional(propagation = Propagation.SUPPORTS,rollbackFor=Exception.class)
    @Override
    public MountainBO queryAllMountainData(long current, long size) {

        final MountainBO mountainBO = new MountainBO();

        final Page<Mountain> page = new Page<>(current,size);

        final QueryWrapper<Mountain> wrapper = new QueryWrapper<>();

        return getMountainBO(current, size, mountainBO, page, wrapper);
    }

    @Transactional(propagation = Propagation.SUPPORTS,rollbackFor=Exception.class)
    @Override
    public MountainBO queryMountainDataByTime(Long current, Long size, Date startTime, Date endTime) {

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        final String sTime = dateFormat.format(startTime);
        final String eTime = dateFormat.format(endTime);

        final MountainBO mountainBO = new MountainBO();

        final Page<Mountain> page = new Page<>(current,size);

        final QueryWrapper<Mountain> wrapper = new QueryWrapper<>();

        wrapper.between("created_time",sTime,eTime);

        return getMountainBO(current, size, mountainBO, page, wrapper);
    }

    @Override
    public List<MountainVO> queryMountainDataByTime(Date startTime, Date endTime) {
        List<MountainVO> vos = null;

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        final String sTime = dateFormat.format(startTime);

        final String eTime = dateFormat.format(endTime);

        final QueryWrapper<Mountain> wrapper = new QueryWrapper<>();

        wrapper.orderByDesc("created_time");

        wrapper.between("created_time",sTime,eTime);

        final List<Mountain> mountains = mountainMapper.selectList(wrapper);
        vos = mToCopy(mountains);
        return vos;
    }


    @Override
    public MountainVO queryMountainInfoById(Integer mountainId) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         DecimalFormat df = new DecimalFormat("0.0");
        Mountain m =null;
        MountainVO mountainVO = new MountainVO();
        final QueryWrapper<Mountain> wrapper = new QueryWrapper<>();
        wrapper.eq("Mountain_id",mountainId);
        wrapper.orderByDesc("created_time");
        final List<Mountain> mountains = mountainMapper.selectList(wrapper);
        if (!mountains.isEmpty()){
            m = mountains.get(0);
            if (m!=null){
                formatData(df, m, mountainVO);

                if (m.getTotalInjectionVolume()!=null){
                    mountainVO.setTotalInjectionVolume(df.format(m.getTotalInjectionVolume()*0.1));
                }

            }

        }
        return mountainVO;
    }

    private static void formatData(DecimalFormat df, Mountain m, MountainVO mountainVO) {
        if(m.getAirHumidity()!=null){
            mountainVO.setAirHumidity(df.format(m.getAirHumidity()*0.1));
        }
        if (m.getSoilHumidity()!=null){
            mountainVO.setSoilHumidity(df.format(m.getSoilHumidity()*0.1));
        }
        if (m.getMountainId()!=null){
            mountainVO.setMountainId(m.getMountainId());
        }
        if (m.getAirTemperature()!=null){
            mountainVO.setAirTemperature(df.format(m.getAirTemperature()*0.1));
        }
        if (m.getSoilTemperature()!=null){
            mountainVO.setSoilTemperature(df.format(m.getSoilTemperature()*0.1));
        }
        if (m.getRainfull()!=null){
            mountainVO.setRainfull(df.format(m.getRainfull()*0.1));
        }
    }

    private MountainBO getMountainBO(Long current, Long size, MountainBO mountainBO, Page<Mountain> page, QueryWrapper<Mountain> wrapper) {
        wrapper.orderByDesc("created_time");

        final Page<Mountain> mountainPage = mountainMapper.selectPage(page, wrapper);

         List<Mountain> records = mountainPage.getRecords();
        final List<MountainVO> voList = mToCopy(records);

        mountainBO.setList(voList);
        mountainBO.setCurrent(current);
        mountainBO.setSize(size);
        mountainBO.setTotal(mountainPage.getTotal());
        return mountainBO;
    }


    private static List<MountainVO> mToCopy(List<Mountain> records){

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        final List<MountainVO> voList = new ArrayList<>();

        final DecimalFormat df = new DecimalFormat("0.0");

        for (Mountain m :
                records) {
            MountainVO mountainVO = new MountainVO();
            formatData(df, m, mountainVO);

            mountainVO.setProbabilityOfLandslide(null);
           if (m.getCreatedTime()!=null){
               mountainVO.setCreatedTime(dateFormat.format(m.getCreatedTime()));
           }
           if (m.getTotalInjectionVolume()!=null){
               mountainVO.setTotalInjectionVolume(df.format(m.getTotalInjectionVolume()*0.1));
           }
            voList.add(mountainVO);
        }
        return voList;
    }
}
